package com.bawei.animator

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.LottieCompositionFactory.fromJson
import com.airbnb.lottie.OnCompositionLoadedListener
import com.bawei.animator.databinding.ActivityMainBinding
import org.json.JSONObject

/**
  三种动画：
 1。帧动画：不建议使用
 2。视图动画：动画结束,恢复到初始状态   一般用于view的入场和出场动画
 3。属性动画：动画结束,保持最后的状态
 4。Lottie动画：
 差值器和估值器：
 1。差值器：8大差值器，动画变化的规律：匀速 加速 减速 弹球效果
 2。估值器：动画变化的值，比如说 0->100  fraction:0-1 贝塞尔曲线


 */
class MainActivity : AppCompatActivity() {
    private lateinit var activityMainBinding: ActivityMainBinding
    private var str = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(activityMainBinding.root)
        //入场
        activityMainBinding.button1.setOnClickListener {
            activityMainBinding.view.visibility = View.VISIBLE
            activityMainBinding.view.startAnimation( AnimationUtils.loadAnimation(this,R.anim.`in`))
            activityMainBinding.view2.visibility = View.VISIBLE
            activityMainBinding.view2.startAnimation( AnimationUtils.loadAnimation(this,R.anim.in_h))


        }
        //出场
        activityMainBinding.button2.setOnClickListener{
            activityMainBinding.view.visibility = View.GONE
            activityMainBinding.view.startAnimation( AnimationUtils.loadAnimation(this,R.anim.out))

            activityMainBinding.view2.visibility = View.GONE
            activityMainBinding.view2.startAnimation( AnimationUtils.loadAnimation(this,R.anim.alpha))
        }
        //属性动画
        activityMainBinding.iv.rotation
        var alpha = ObjectAnimator.ofFloat(activityMainBinding.iv,"alpha",1f,0f,1f)
        var scaleX = ObjectAnimator.ofFloat(activityMainBinding.iv,"scaleX",1f,2f,1f)
        var scaleY = ObjectAnimator.ofFloat(activityMainBinding.iv,"scaleY",1f,2f,1f)
        var translationX = ObjectAnimator.ofFloat(activityMainBinding.iv,"translationX",0f,300f,100f)
        var rotation = ObjectAnimator.ofFloat(activityMainBinding.iv,"rotation",0f,360f)
        var animatorSet = AnimatorSet()
        animatorSet.playTogether(alpha,scaleX,scaleY,translationX,rotation)
        animatorSet.duration = 3000
        animatorSet.start()

        //加载服务器的json动画：Lottie
        LottieComposition.Factory
            .fromJsonString(str) { composition ->
                if (composition != null) {
                    activityMainBinding.animationView.setComposition(composition)
                }
                activityMainBinding.animationView.playAnimation()
            }


    }
}